# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=ffms2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.40
pkgrel=5
pkgdesc="A cross-platform wrapper library around libav/FFmpeg (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url="https://github.com/FFMS/ffms2/"
license=('MIT')
options=('staticlibs' 'strip')
depends=("${MINGW_PACKAGE_PREFIX}-ffmpeg")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/FFMS/ffms2/archive/${pkgver}.tar.gz"
        "0001-ffmsindex-add-municode-ldflag.patch"
        "https://github.com/FFMS/ffms2/commit/586d87de3f896d0c4ff01b21f572375e11f9c3f1.patch"
        "https://github.com/FFMS/ffms2/commit/45673149e9a2f5586855ad472e3059084eaa36b1.patch")
sha256sums=('82e95662946f3d6e1b529eadbd72bed196adfbc41368b2d50493efce6e716320'
            '59e2150127dbdf2645bab6cbbd8842cc58dd33ce12238e76d1b2916d9d051f56'
            'd5ec6ee44bf8f0a2bd943c1c61bace33b7b80559137b71fe5c4af5e7dd9ac7f8'
            'ecef89ce1bb4158181c96eecdbc2c37e49a880a88e72a271b54ce98832dcf049')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}/0001-ffmsindex-add-municode-ldflag.patch"

  # ffmpeg 5
  patch -Np1 -i "${srcdir}/586d87de3f896d0c4ff01b21f572375e11f9c3f1.patch"
  patch -Np1 -i "${srcdir}/45673149e9a2f5586855ad472e3059084eaa36b1.patch"

  mkdir -p src/config
  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --disable-debug \
    --disable-static \
    --enable-shared

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
